package com.oa.srms.apps.dao.dormitory.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.dormitory.IBedInfoDao;
import com.oa.srms.apps.entity.dormitory.BedInfoValue;

public class BedInfoDaoImpl extends HibernateDaoSupport implements IBedInfoDao {
	
	@Autowired
	private List<BedInfoValue> list;
	
	private int totalNumber;

	@Override
	public Integer addBedInfo(BedInfoValue bedInfoValue) throws Exception {
		if (bedInfoValue != null) {
			this.getHibernateTemplate().save(bedInfoValue);
		}
		return bedInfoValue.getId();
	}

	@Override
	public void editBedInfo(BedInfoValue bedInfoValue) throws Exception {
		if (bedInfoValue != null) {
			this.getHibernateTemplate().update(bedInfoValue);
		}
	}

	@Override
	public BedInfoValue queryById(Integer id) throws Exception {
		return (BedInfoValue) this.getHibernateTemplate().get(BedInfoValue.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BedInfoValue> queryBedInfo(final String building, final String roomNo,
			final Integer grade, final Integer classNo, final Integer states, final Integer rows,
			final Integer pageNo) throws Exception {
		String hqlStr = "SELECT biv FROM BedInfoValue biv WHERE 1=1";
		if (building != null) {
			hqlStr += " AND biv.building = ?";
		}
		if (roomNo != null) {
			hqlStr += " AND biv.roomNo = ? ";
		}
		if (grade != null) {
			hqlStr += " AND biv.grade = ? ";
		}
		if (classNo != null) {
			hqlStr += " AND biv.classNo = ? ";
		}
		if (states != null) {
			hqlStr += " AND biv.states = ? ";
		}
		hqlStr += " AND biv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (building != null) {
					query.setString(++index, building);
				}
				if (roomNo != null) {
					query.setString(++index, roomNo);
				}
				if (grade != null) {
					query.setInteger(++index, grade);
				}
				if (classNo != null) {
					query.setInteger(++index, classNo);
				}
				if (states != null) {
					query.setInteger(++index, states);
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer getTotalPageNumber(final String building, final String roomNo,
			final Integer grade, final Integer classNo, final Integer states, Integer rows) {
		int totalPageNumber = 0;
		String hqlStr = "SELECT biv FROM BedInfoValue biv WHERE 1=1";
		if (building != null) {
			hqlStr += " AND biv.building = ?";
		}
		if (roomNo != null) {
			hqlStr += " AND biv.roomNo = ? ";
		}
		if (grade != null) {
			hqlStr += " AND biv.grade = ? ";
		}
		if (classNo != null) {
			hqlStr += " AND biv.classNo = ? ";
		}
		if (states != null) {
			hqlStr += " AND biv.states = ? ";
		}
		hqlStr += " AND biv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (building != null) {
					query.setString(++index, building);
				}
				if (roomNo != null) {
					query.setString(++index, roomNo);
				}
				if (grade != null) {
					query.setInteger(++index, grade);
				}
				if (classNo != null) {
					query.setInteger(++index, classNo);
				}
				if (states != null) {
					query.setInteger(++index, states);
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		totalNumber = list.size();
		if (totalNumber <= rows) {
			totalPageNumber = 1;
		} else {
			if (totalNumber % rows == 0) {
				totalPageNumber = totalNumber / rows;
			} else {
				totalPageNumber = totalNumber / rows + 1;
			}
		}
		return totalPageNumber;
	}

	@Override
	public Integer getTotalNumber() {
		return totalNumber;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BedInfoValue> queryBedInfo(final Integer grade, final Integer classNo,
			final String gender, final Integer states) {
		String hqlStr = "FROM BedInfoValue biv WHERE 1=1";
		if (grade != null) {
			hqlStr += " AND biv.grade = ? ";
		}
		if (classNo != null) {
			hqlStr += " AND biv.classNo = ? ";
		}
		if (gender != null) {
			hqlStr += " AND biv.gender = ? ";
		}
		if (states != null) {
			hqlStr += " AND biv.states = ? ";
		}
		hqlStr += " AND biv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (grade != null) {
					query.setInteger(++index, grade);
				}
				if (classNo != null) {
					query.setInteger(++index, classNo);
				}
				if (gender != null) {
					query.setString(++index, gender);
				}
				if (states != null) {
					query.setInteger(++index, states);
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		return list;
	}


}
